<!--
*@Author: QWP
*@Description: 业务短信配置编辑
*@Date 2024-09-12 14:52
-->
<template>
  <u-dialog v-bind="$attrs"
    destroyOnClose
    :confirmLoading="loading"
    @close="dialogClose"
    @submit="submit">
    <u-form :elements="formEls"
      :model="form"
      :gutter="0"
      :loading="loading"
      ref="formRef">
    </u-form>
  </u-dialog>
</template>

<script lang='ts' setup>
import { ref, reactive, computed, onMounted, h, watch, useAttrs } from 'vue' 
import { _IFormEl } from '@/src/components/common/form/interface';
import { FormInstance, FormItemProps } from 'ant-design-vue';
import { nullC, alertC, messageC } from '@/util/common'
import { cloneDeep } from 'lodash'
import { busiTypes } from './datas'
import * as smsCApi from '@/service/system/smsConfig/index.ts'

const props = defineProps<{
  current?: object,
}>()

const emits = defineEmits(['success'])

const loading = ref<boolean>(false)
const formRef = ref<FormInstance>()
const form = ref({})
const formItemProps: FormItemProps & _IFormEl = {
  labelWidth: '80px',
  colon: false
}
const formEls: Array<_IFormEl> = [
  { label: '业务类型', key: 'moudletype', col: { span: 24 }, element: 'select', 
    formItemProps: { ...formItemProps, required: true } ,
    componentProps: {
      options: busiTypes
    }
  },
  { label: '是否启用', key: 'isenable', col: { span: 24 }, element: 'select', 
    formItemProps: { ...formItemProps, required: true },
    componentProps: {
      options: [
        { label: '启用', value: 1 },
        { label: '停用', value: 2 },
      ]
    }
  },
]

// 保存提交
const submit = async(type?: string) => {
  try {
    await formRef.value.form.validate()
  } catch (error) {
    alertC("请先表单信息填写完毕!");
    return
  }
  
  const url = form.value.keyid ? 'update' : 'add';
  
  // await alertC("确认保存？", 'confirm')

  loading.value = true;
  try {
    const res = await smsCApi[url](form.value)
    if(res.code == 0) {
        messageC(res.msg, "success")
        dialogClose()
        emits('success')
    }
    else {
      messageC(res.msg, "error")
    }
  } catch (error) { }
  loading.value = false;
}

// 获取回显信息
const echoInfo = async () => {
  loading.value = true;
  try {
    const res = await smsCApi.query(props.current['keyid']);
    if(res.code == 0) {
      let data = res.data;
      form.value = res.data;
      form.value.fkunitsid0 = [];
      (data.acceptList || []).forEach(item => {
        if(item.stats == 1) {
          form.value.fkunitsid1 = item.fkunitsid
        }
        else {
          form.value.fkunitsid0.push(item.fkunitsid)
        }
      })

      let fileList = form.value['fileList'] || [];

      form.value.fileList = fileList.filter(i => i.fileAttribute == 1)
      form.value.fj = fileList.filter(i => i.fileAttribute == 2)
      form.value.xgwd = fileList.filter(i => i.fileAttribute == 3)
    }
  } catch (error) { }
  loading.value = false;
}

const dialogClose = () => {
  form.value = {};
  formRef.value.form.resetFields();
}

</script>

<style lang='scss' >
</style>